package com.listen.common;

import java.io.IOException;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.net.ssl.SSLContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.SystemDefaultRoutePlanner;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;

import com.listen.vo.HeadParmsVO;

public class HttpClientProxy {
	private RequestConfig requestConfig = null;
    
	/**
     * 创建一个可以访问Https类型URL的工具类，返回一个CloseableHttpClient实例
     */
    public CloseableHttpClient createSSLClientDefault(){
        try {
            SSLContext sslContext=new SSLContextBuilder().loadTrustMaterial(
                    null,new TrustStrategy() {
                        //信任所有
                        public boolean isTrusted(X509Certificate[] chain, String authType)
                                throws CertificateException {
                            return true;
                        }
                    }).build();
            SSLConnectionSocketFactory sslsf=new SSLConnectionSocketFactory(sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }
	
	//通过系统代理发送HttpGet请求
	public CloseableHttpResponse sendProxyHttpGet(CloseableHttpClient httpClient,HeadParmsVO heads,
			String path,String proxyInfo) {
        CloseableHttpResponse response = null;
		HttpGet httpGet = new HttpGet(path);
        try {
        	//1设置头部参数
        	setHeadParms(httpGet,heads);
        	
        	//3设置config
    		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).setRedirectsEnabled(false).build();
    		httpGet.setConfig(requestConfig);
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//2解析代理IP
        		String proxyIp = proxyInfo.split(":")[0];
        		String proxyPort = proxyInfo.split(":")[1];
        		
        		//3设置系统代理
        		System.setProperty("http.proxyHost",proxyIp);
        		System.setProperty("http.proxyPort",proxyPort);
        		
        		SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
        		httpClient = HttpClients.custom()
            	        .setRoutePlanner(routePlanner)
            	        .build();
        	}else {
        		
        		// 创建默认的httpClient实例.  
                httpClient = HttpClients.createDefault();  
        	}
        	
        	// 执行请求  
            response = httpClient.execute(httpGet);
        	
        } catch (Exception e) {
            e.printStackTrace();  
        }
        return response;  
    }
	
	//通过代理发送HttpGet请求
	public CloseableHttpResponse sendHttpGet(CloseableHttpClient httpClient,HeadParmsVO heads,
			String path,String proxyInfo) {
        CloseableHttpResponse response = null;
		HttpGet httpGet = new HttpGet(path);
        try {
        	
        	//1设置头部参数
        	setHeadParms(httpGet,heads);
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//2解析代理IP
        		String proxyIp = proxyInfo.split(":")[0];
        		int proxyPort = Integer.parseInt(proxyInfo.split(":")[1]);
        		
        		//3设置代理IP
        		HttpHost proxy = new HttpHost(proxyIp,proxyPort,"http");
        		requestConfig = RequestConfig.custom().setProxy(proxy).setSocketTimeout(8000).setConnectTimeout(8000).setRedirectsEnabled(false).build();
        	}else {
        		//3设置config
        		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).setRedirectsEnabled(false).build();
        	}
        	httpGet.setConfig(requestConfig);
        	// 创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault();  
            // 执行请求  
            response = httpClient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();  
        }
        return response;  
    }
	
	//通过系统代理发送HttpGet请求
	public CloseableHttpResponse sendProxyHttpGet(CloseableHttpClient httpClient,String path,String proxyInfo) {
        CloseableHttpResponse response = null;
        HttpGet httpGet = new HttpGet(path);
        try {
        	requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).setRedirectsEnabled(false).build();
    		httpGet.setConfig(requestConfig);
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//1解析代理IP
        		String proxyIp = proxyInfo.split(":")[0];
        		String proxyPort = proxyInfo.split(":")[1];
        		//2设置系统代理
        		System.setProperty("http.proxyHost",proxyIp);
        		System.setProperty("http.proxyPort",proxyPort);
        		SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
        		httpClient = HttpClients.custom()
            	        .setRoutePlanner(routePlanner)
            	        .build();
        	}else {
        		
        		//1创建默认的httpClient实例.  
                httpClient = HttpClients.createDefault();  
        	}
        	
        	//2执行请求  
            response = httpClient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();  
        }
        return response;  
    }
	
	//通过代理发送HttpGet请求
	public CloseableHttpResponse sendHttpGet(CloseableHttpClient httpClient,String path,String proxyInfo) {
        CloseableHttpResponse response = null;
        HttpGet httpGet = new HttpGet(path);
        try {
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//1解析代理IP
        		String proxyIp = proxyInfo.split(":")[0];
        		int proxyPort = Integer.parseInt(proxyInfo.split(":")[1].trim());
        		
        		//2设置代理IP端口
        		HttpHost proxy = new HttpHost(proxyIp,proxyPort,"http");
        		requestConfig = RequestConfig.custom().setProxy(proxy).setSocketTimeout(8000).setRedirectsEnabled(false).setConnectTimeout(8000).build();
        	}else {
        		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).setRedirectsEnabled(false).build();
        	}
        	
        	httpGet.setConfig(requestConfig);
        	
        	//3创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault();  
            
            //4执行请求  
            response = httpClient.execute(httpGet);
        } catch (Exception e) {
            e.printStackTrace();  
        }
        return response;  
    }
	
	/** 
     * 通过代理发送 post请求 
     * @param path 地址 
     */ 
	public CloseableHttpResponse sendProxyHttpPost(CloseableHttpClient httpClient,HeadParmsVO heads,
					String path,String proxyInfo) {
        CloseableHttpResponse response = null;
        HttpPost httpPost = new HttpPost(path);
        try {
        	//1设置头部参数
        	setHeadParms(httpPost,heads);
        	
        	//3设置config
    		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
    		httpPost.setConfig(requestConfig);
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//2解析代理ip端口
        		String proxyIp = proxyInfo.split(":")[0];
        		String proxyPort = proxyInfo.split(":")[1];
        		
        		//3设置系统代理
        		System.setProperty("http.proxyHost",proxyIp);
        		System.setProperty("http.proxyPort",proxyPort);
        		
        		SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
        		httpClient = HttpClients.custom()
            	        .setRoutePlanner(routePlanner)
            	        .build();
        	}else {
        		//4创建默认的httpClient实例.  
                httpClient = HttpClients.createDefault();  
        	}
            //5执行请求  
            response = httpClient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();  
        }
        return response;  
    }
	
	/** 
     * 通过代理发送 post请求 
     * @param path 地址 
     */ 
	public CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient,HeadParmsVO heads,
					String path,String proxyInfo) {
        CloseableHttpResponse response = null;
        HttpPost httpPost = new HttpPost(path);
        try {
        	//1设置头部参数
        	setHeadParms(httpPost,heads);
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		
        		//2解析代理ip端口
        		String proxyIp = proxyInfo.split(":")[0];
        		int proxyPort = Integer.parseInt(proxyInfo.split(":")[1]);
        		
        		//3设置代理
        		HttpHost proxy = new HttpHost(proxyIp,proxyPort,"http");
        		requestConfig = RequestConfig.custom().setProxy(proxy).setSocketTimeout(8000).setConnectTimeout(8000).build();
        	}else {
        		//3设置config
        		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
        	}
        	
        	httpPost.setConfig(requestConfig);
        	
        	//4创建默认的httpClient实例.  
            httpClient = HttpClients.createDefault();  
            
            //5执行请求  
            response = httpClient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();  
        }
        return response;  
    }
	
	/** 
     * 发送 post请求 
     * @param proxyInfo 代理ip和端口(格式：ip:port)
     * @param params 参数(格式:key1=value1&key2=value2) 
     */  
    public CloseableHttpResponse sendProxyHttpPost(CloseableHttpClient httpClient,HeadParmsVO heads,
    			String path,String params,String proxyInfo) {
        CloseableHttpResponse response = null;
        HttpPost httpPost = new HttpPost(path);
        try {
        	//1设置头部参数
        	setHeadParms(httpPost,heads);
        	
        	//2设置方法体参数  
    		StringEntity stringEntity = new StringEntity(params, "UTF-8");
    		httpPost.setEntity(stringEntity);
        	
    		//3设置代理
        	requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
        	httpPost.setConfig(requestConfig); 
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//3解析代理ip端口
        		String proxyIp = proxyInfo.split(":")[0];
        		String proxyPort = proxyInfo.split(":")[1];
        		
        		//3设置系统代理
        		System.setProperty("http.proxyHost",proxyIp);
        		System.setProperty("http.proxyPort",proxyPort);
        		
        		SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
        		httpClient = HttpClients.custom()
            	        .setRoutePlanner(routePlanner)
            	        .build();
        	}else {
        		//5产生httpClient实例
            	httpClient = HttpClients.createDefault();
        	}
            
        	//6执行请求  
            response = httpClient.execute(httpPost);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return response; 
    } 
	
	/** 
     * 发送 post请求 
     * @param proxyInfo 代理ip和端口(格式：ip:port)
     * @param params 参数(格式:key1=value1&key2=value2) 
     */  
    public CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient,HeadParmsVO heads,
    			String path,String params,String proxyInfo) {
        CloseableHttpResponse response = null;
        HttpPost httpPost = new HttpPost(path);
        try {
        	//1设置头部参数
        	setHeadParms(httpPost,heads);
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//2解析代理IP端口
        		String proxyIp = proxyInfo.split(":")[0];
        		int proxyPort = Integer.parseInt(proxyInfo.split(":")[1]);
        		
        		//3设置方法体参数  
        		StringEntity stringEntity = new StringEntity(params, "UTF-8");
        		httpPost.setEntity(stringEntity);
        		
        		//4设置代理
        		HttpHost proxy = new HttpHost(proxyIp,proxyPort,"http");
        		requestConfig = RequestConfig.custom().setProxy(proxy).setSocketTimeout(8000).setConnectTimeout(8000).build();
        	}else {
        		//3设置方法体参数  
        		StringEntity stringEntity = new StringEntity(params, "UTF-8");
        		httpPost.setEntity(stringEntity);
        		
        		//4设置代理
        		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
        	}
            
            //5产生httpClient实例
            httpClient = HttpClients.createDefault();
            httpPost.setConfig(requestConfig);  
            
            //6执行请求  
            response = httpClient.execute(httpPost);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return response; 
    } 
	
    /** 
     * 发送 post请求 
     * @param maps 参数
     */  
    public CloseableHttpResponse sendProxyHttpPost(CloseableHttpClient httpClient,HeadParmsVO heads,
			String path,Map<String, String> maps,String proxyInfo) {
    	CloseableHttpResponse response = null;
		HttpPost httpPost = new HttpPost(path);
         
        try {
        	//1设置头部参数
        	setHeadParms(httpPost,heads);
        	
        	//3设置请求参数    
    		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
    		for (String key : maps.keySet()) {  
    			nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
    		} 
    		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        	
    		//4设置config
    		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
    		httpPost.setConfig(requestConfig);
    		
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//2解析代理IP端口
        		String proxyIp = proxyInfo.split(":")[0];
        		String proxyPort = proxyInfo.split(":")[1];
        		
        		//3设置系统代理
        		System.setProperty("http.proxyHost",proxyIp);
        		System.setProperty("http.proxyPort",proxyPort);
        		
        		SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(ProxySelector.getDefault());
        		httpClient = HttpClients.custom()
            	        .setRoutePlanner(routePlanner)
            	        .build();
        	}else {
        		//5产生httpClient实例
        		httpClient = HttpClients.createDefault();
        	}
        	
            //6执行请求  
            response = httpClient.execute(httpPost);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return response;  
    }
    
    /** 
     * 发送 post请求 
     * @param maps 参数
     */  
    public CloseableHttpResponse sendHttpPost(CloseableHttpClient httpClient,HeadParmsVO heads,
			String path,Map<String, String> maps,String proxyInfo) {
    	CloseableHttpResponse response = null;
		HttpPost httpPost = new HttpPost(path);
         
        try {
        	//1设置头部参数
        	setHeadParms(httpPost,heads);
        	
        	if(StringUtils.isNotBlank(proxyInfo)) {
        		//2解析代理IP端口
        		String proxyIp = proxyInfo.split(":")[0];
        		int proxyPort = Integer.parseInt(proxyInfo.split(":")[1]);
        		//3设置请求参数    
        		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        		for (String key : maps.keySet()) {  
        			nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        		} 
        		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        		
        		//4设置代理
        		HttpHost proxy = new HttpHost(proxyIp,proxyPort,"http");
        		requestConfig = RequestConfig.custom().setProxy(proxy).setSocketTimeout(8000).setConnectTimeout(8000).build();
        	}else {
        		//3设置请求参数    
        		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
        		for (String key : maps.keySet()) {  
        			nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));  
        		} 
        		httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
        		
        		//4设置config
        		requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).build();
        	}
        	
            
            //5产生httpClient实例
            httpClient = HttpClients.createDefault();
            httpPost.setConfig(requestConfig);  
            
            //6执行请求  
            response = httpClient.execute(httpPost);
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return response;  
    }

	/**
	 * 发送Get请求Https
	 * @return
	 */
	public CloseableHttpResponse sendHttpsGet(CloseableHttpClient httpClient,HeadParmsVO heads,String path) {
		CloseableHttpResponse response = null;
		HttpGet httpGet = new HttpGet(path);
		try {
			this.setHeadParms(httpGet,heads);
			// 创建默认的httpClient实例.
			PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
			DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
			httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
			httpGet.setConfig(requestConfig);
			// 执行请求
			response = httpClient.execute(httpGet);
			//entity = response.getEntity();
			//responseContent = EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

	/**
	 * 发送Post请求Https
	 * @return
	 */
	public CloseableHttpResponse sendHttpsPost(CloseableHttpClient httpClient,HeadParmsVO heads,String path,Map<String,String> maps) {
		CloseableHttpResponse response = null;
		HttpPost httpPost = new HttpPost(path);
		try {
			this.setHeadParms(httpPost,heads);

			//3设置请求参数
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
			for (String key : maps.keySet()) {
				nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
			}
			httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));

			// 创建默认的httpClient实例.
			PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpPost.getURI().toString()));
			DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
			httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
			httpPost.setConfig(requestConfig);
			// 执行请求
			response = httpClient.execute(httpPost);
			//entity = response.getEntity();
			//responseContent = EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

	/**
	 * 发送Post请求Https
	 * @return
	 */
	public CloseableHttpResponse sendHttpsPut(CloseableHttpClient httpClient,HeadParmsVO heads,String path,Map<String,String> maps) {
		CloseableHttpResponse response = null;
		HttpPut httpPut = new HttpPut(path);
		try {
			this.setHeadParms(httpPut,heads);

			//3设置请求参数
			if (maps != null){
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				for (String key : maps.keySet()) {
					nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
				}
				httpPut.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
			}

			// 创建默认的httpClient实例.
			PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpPut.getURI().toString()));
			DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
			httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
			httpPut.setConfig(requestConfig);
			// 执行请求
			response = httpClient.execute(httpPut);
			//entity = response.getEntity();
			//responseContent = EntityUtils.toString(entity, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return response;
	}

    /**
	 * 根据给定的链接获取所有的重定向位置
	 * @param link 给定的链接
	 * @return
	 * @throws ClientProtocolException
	 * @throws IOException
	 */
	public List<URI> getAllRedirectLocations(String link) throws ClientProtocolException, IOException{
		List<java.net.URI> redirectLocations = null;
		CloseableHttpResponse response = null;
		try{
			HttpClientContext context = HttpClientContext.create();
			HttpGet httpGet = new HttpGet(link);
			HeadParmsVO heads = new HeadParmsVO();
		    heads.setAccept("text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3");
		    heads.setAcceptEncoding("gzip, deflate");
		    heads.setAcceptLanguage("zh-CN,zh;q=0.9,en;q=0.8");
		    heads.setProxyConnection("keep-alive");
		    heads.setUpgradeInsecureRequests("1");
		    heads.setCookie("PageInfo=IDCard=36242719880108442X&Name=%e9%83%ad%e7%ba%b6%e5%a8%9f&Sex=%e5%a5%b3&UserIDAll=51809&orgUserIDAll=&UserName=&Tel=");
		    heads.setHost("www.sljls.cn");
			setHeadParms(httpGet,heads);
			CloseableHttpClient httpClient =  HttpClients.createDefault();
			response = httpClient.execute(httpGet, context);
			int status = response.getStatusLine().getStatusCode();
			System.out.println(status);
			// 获取所有的重定向位置
			redirectLocations = context.getRedirectLocations();
		} finally{
			if(response!=null){
				response.close();
			}
		}
		return redirectLocations;
	}
    
    
	private void setHeadParms(HttpPost httpPost,HeadParmsVO heads) {
		if(StringUtils.isNotBlank(heads.getAccept())) {
			httpPost.setHeader("Accept",heads.getAccept());
		}if(StringUtils.isNotBlank(heads.getCookie())) {
			httpPost.setHeader("Cookie",heads.getCookie());
		}if(StringUtils.isNotBlank(heads.getContentType())) {
			httpPost.setHeader("Content-Type",heads.getContentType());
		}if(StringUtils.isNotBlank(heads.getxRequestedWith())) {
			httpPost.setHeader("X-Requested-With",heads.getxRequestedWith());
		}if(StringUtils.isNotBlank(heads.getUserAgent())) {
			httpPost.setHeader("User-Agent",heads.getUserAgent());
		}if(StringUtils.isNotBlank(heads.getAcceptEncoding())) {
			httpPost.setHeader("Accept-Encoding",heads.getAcceptEncoding());
		}if(StringUtils.isNotBlank(heads.getHost())) {
			httpPost.setHeader("Host",heads.getHost());
		}if(StringUtils.isNotBlank(heads.getOrigin())) {
			httpPost.setHeader("Origin",heads.getOrigin());
		}if(StringUtils.isNotBlank(heads.getReferer())) {
			httpPost.setHeader("Referer",heads.getReferer());
		}if(StringUtils.isNotBlank(heads.getDNT())) {
			httpPost.setHeader("DNT",heads.getDNT());
		}if(StringUtils.isNotBlank(heads.getUpgradeInsecureRequests())) {
			httpPost.setHeader("Upgrade-Insecure-Requests",heads.getUpgradeInsecureRequests());
		}if(StringUtils.isNotBlank(heads.getAcceptLanguage())) {
			httpPost.setHeader("Accept-Language",heads.getAcceptLanguage());
		}if(StringUtils.isNotBlank(heads.getCacheControl())) {
			httpPost.setHeader("Cache-Control",heads.getCacheControl());
		}if(StringUtils.isNotBlank(heads.getConnection())) {
			httpPost.setHeader("Connection",heads.getConnection());
		}if(StringUtils.isNotBlank(heads.getProxyConnection())) {
			httpPost.setHeader("Proxy-Connection",heads.getProxyConnection());
		}if(StringUtils.isNotBlank(heads.getPragma())) {
			httpPost.setHeader("Pragma",heads.getPragma());
		}if(StringUtils.isNotBlank(heads.getIfRange())) {
			httpPost.setHeader("If-Range",heads.getIfRange());
		}if(StringUtils.isNotBlank(heads.getRange())) {
			httpPost.setHeader("Range",heads.getRange());
		}if(StringUtils.isNotBlank(heads.getSecFetchMode())){
			httpPost.setHeader("Sec-Fetch-Mode",heads.getSecFetchMode());
		}if(StringUtils.isNotBlank(heads.getSecFetchSite())){
			httpPost.setHeader("Sec-Fetch-Site",heads.getSecFetchSite());
		}if(StringUtils.isNotBlank(heads.getAuthorization())){
			httpPost.setHeader("authorization",heads.getAuthorization());
		}if(StringUtils.isNotBlank(heads.getIsAjax())){
			httpPost.setHeader("isAjax",heads.getIsAjax());
		}if(StringUtils.isNotBlank(heads.getAccess_Control_Allow_Origin())){
			httpPost.setHeader("Access-Control-Allow-Origin",heads.getAccess_Control_Allow_Origin());
		}

	}

	private void setHeadParms(HttpPut httpPost,HeadParmsVO heads) {
		if(StringUtils.isNotBlank(heads.getAccept())) {
			httpPost.setHeader("Accept",heads.getAccept());
		}if(StringUtils.isNotBlank(heads.getCookie())) {
			httpPost.setHeader("Cookie",heads.getCookie());
		}if(StringUtils.isNotBlank(heads.getContentType())) {
			httpPost.setHeader("Content-Type",heads.getContentType());
		}if(StringUtils.isNotBlank(heads.getxRequestedWith())) {
			httpPost.setHeader("X-Requested-With",heads.getxRequestedWith());
		}if(StringUtils.isNotBlank(heads.getUserAgent())) {
			httpPost.setHeader("User-Agent",heads.getUserAgent());
		}if(StringUtils.isNotBlank(heads.getAcceptEncoding())) {
			httpPost.setHeader("Accept-Encoding",heads.getAcceptEncoding());
		}if(StringUtils.isNotBlank(heads.getHost())) {
			httpPost.setHeader("Host",heads.getHost());
		}if(StringUtils.isNotBlank(heads.getOrigin())) {
			httpPost.setHeader("Origin",heads.getOrigin());
		}if(StringUtils.isNotBlank(heads.getReferer())) {
			httpPost.setHeader("Referer",heads.getReferer());
		}if(StringUtils.isNotBlank(heads.getDNT())) {
			httpPost.setHeader("DNT",heads.getDNT());
		}if(StringUtils.isNotBlank(heads.getUpgradeInsecureRequests())) {
			httpPost.setHeader("Upgrade-Insecure-Requests",heads.getUpgradeInsecureRequests());
		}if(StringUtils.isNotBlank(heads.getAcceptLanguage())) {
			httpPost.setHeader("Accept-Language",heads.getAcceptLanguage());
		}if(StringUtils.isNotBlank(heads.getCacheControl())) {
			httpPost.setHeader("Cache-Control",heads.getCacheControl());
		}if(StringUtils.isNotBlank(heads.getConnection())) {
			httpPost.setHeader("Connection",heads.getConnection());
		}if(StringUtils.isNotBlank(heads.getProxyConnection())) {
			httpPost.setHeader("Proxy-Connection",heads.getProxyConnection());
		}if(StringUtils.isNotBlank(heads.getPragma())) {
			httpPost.setHeader("Pragma",heads.getPragma());
		}if(StringUtils.isNotBlank(heads.getIfRange())) {
			httpPost.setHeader("If-Range",heads.getIfRange());
		}if(StringUtils.isNotBlank(heads.getRange())) {
			httpPost.setHeader("Range",heads.getRange());
		}if(StringUtils.isNotBlank(heads.getSecFetchMode())){
			httpPost.setHeader("Sec-Fetch-Mode",heads.getSecFetchMode());
		}if(StringUtils.isNotBlank(heads.getSecFetchSite())){
			httpPost.setHeader("Sec-Fetch-Site",heads.getSecFetchSite());
		}if(StringUtils.isNotBlank(heads.getAuthorization())){
			httpPost.setHeader("authorization",heads.getAuthorization());
		}if(StringUtils.isNotBlank(heads.getIsAjax())){
			httpPost.setHeader("isAjax",heads.getIsAjax());
		}if(StringUtils.isNotBlank(heads.getAccess_Control_Allow_Origin())){
			httpPost.setHeader("Access-Control-Allow-Origin",heads.getAccess_Control_Allow_Origin());
		}
	}


	private void setHeadParms(HttpGet httpGet,HeadParmsVO heads) {
		if(StringUtils.isNotBlank(heads.getAccept())) {
			httpGet.setHeader("Accept",heads.getAccept());
		}if(StringUtils.isNotBlank(heads.getCookie())) {
			httpGet.setHeader("Cookie",heads.getCookie());
		}if(StringUtils.isNotBlank(heads.getContentType())) {
			httpGet.setHeader("Content-Type",heads.getContentType());
		}if(StringUtils.isNotBlank(heads.getxRequestedWith())) {
			httpGet.setHeader("X-Requested-With",heads.getxRequestedWith());
		}if(StringUtils.isNotBlank(heads.getUserAgent())) {
			httpGet.setHeader("User-Agent",heads.getUserAgent());
		}if(StringUtils.isNotBlank(heads.getAcceptEncoding())) {
			httpGet.setHeader("Accept-Encoding",heads.getAcceptEncoding());
		}if(StringUtils.isNotBlank(heads.getHost())) {
			httpGet.setHeader("Host",heads.getHost());
		}if(StringUtils.isNotBlank(heads.getOrigin())) {
			httpGet.setHeader("Origin",heads.getOrigin());
		}if(StringUtils.isNotBlank(heads.getReferer())) {
			httpGet.setHeader("Referer",heads.getReferer());
		}if(StringUtils.isNotBlank(heads.getDNT())) {
			httpGet.setHeader("DNT",heads.getDNT());
		}if(StringUtils.isNotBlank(heads.getUpgradeInsecureRequests())) {
			httpGet.setHeader("Upgrade-Insecure-Requests",heads.getUpgradeInsecureRequests());
		}if(StringUtils.isNotBlank(heads.getAcceptLanguage())) {
			httpGet.setHeader("Accept-Language",heads.getAcceptLanguage());
		}if(StringUtils.isNotBlank(heads.getCacheControl())) {
			httpGet.setHeader("Cache-Control",heads.getCacheControl());
		}if(StringUtils.isNotBlank(heads.getConnection())) {
			httpGet.setHeader("Connection",heads.getConnection());
		}if(StringUtils.isNotBlank(heads.getProxyConnection())) {
			httpGet.setHeader("Proxy-Connection",heads.getProxyConnection());
		}if(StringUtils.isNotBlank(heads.getPragma())) {
			httpGet.setHeader("Pragma",heads.getPragma());
		}if(StringUtils.isNotBlank(heads.getIfRange())) {
			httpGet.setHeader("If-Range",heads.getIfRange());
		}if(StringUtils.isNotBlank(heads.getRange())) {
			httpGet.setHeader("Range",heads.getRange());
		}if(StringUtils.isNotBlank(heads.getSecFetchMode())){
			httpGet.setHeader("Sec-Fetch-Mode",heads.getSecFetchMode());
		}if(StringUtils.isNotBlank(heads.getSecFetchSite())){
			httpGet.setHeader("Sec-Fetch-Site",heads.getSecFetchSite());
		}if(StringUtils.isNotBlank(heads.getAuthorization())){
			httpGet.setHeader("Authorization",heads.getAuthorization());
		}if(StringUtils.isNotBlank(heads.getIsAjax())){
			httpGet.setHeader("isAjax",heads.getIsAjax());
		}if(StringUtils.isNotBlank(heads.getAccess_Control_Allow_Origin())){
			httpGet.setHeader("Access-Control-Allow-Origin",heads.getAccess_Control_Allow_Origin());
		}
	}
	
	public void closeResource(CloseableHttpResponse response,CloseableHttpClient client){
    	try {
    		if(response !=null)
    			response.close();
			if(client != null)
				client.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
	
	/*public static void main(String[] args) throws ClientProtocolException, IOException {
		// 输入URL
		String link = "http://wdy.sljls.cn/UserLogin.aspx?from=qazwsx&id=36242719880108442X";
		HttpClientProxy demo = new HttpClientProxy();
		List<URI> allRedirectLocations = demo.getAllRedirectLocations(link);
		if(allRedirectLocations!=null){
			System.out.println(link);
			for(URI uri : allRedirectLocations){
				System.out.println("|\nv\n" + uri.toASCIIString());
			}
		} else{
			System.out.println("Not found!");
		}

	}*/
	public static void main(String[] args) {
		String url = "http://wdy.sljls.cn/UserLogin.aspx?from=qazwsx&id=36242719880108442X";
		System.out.println(url.substring(0,url.indexOf("\\")+1));
	}
}
